import 'package:shared_preferences/shared_preferences.dart';
import 'package:smart_ai_dart/utils/log_utils.dart';

class StorageUtils {
  late SharedPreferences _prefs;
  bool _isInitialized = false;
  static final StorageUtils shared = StorageUtils._internal();

  factory StorageUtils() {
    return shared;
  }

  StorageUtils._internal();

  Future<void> init() async {
    if (_isInitialized) {
      LogUtils.shared.w('StorageUtils has already been initialized!');
      return;
    }
    _prefs = await SharedPreferences.getInstance();
    _isInitialized = true;
    LogUtils.shared.i('StorageUtils initialized successfully');
  }

  void _checkInitialization() {
    if (!_isInitialized) {
      throw Exception(
          'StorageUtils not initialized. Call StorageUtils.shared.init() first.');
    }
  }

  // Store string
  Future<bool> setString(String key, String value) async {
    _checkInitialization();
    try {
      final result = await _prefs.setString(key, value);
      LogUtils.shared.i('Stored string: $key = $value');
      return result;
    } catch (e) {
      LogUtils.shared.e('Error storing string: $e');
      rethrow;
    }
  }

  // 获取字符串
  String? getString(String key) {
    _checkInitialization();
    try {
      final value = _prefs.getString(key);
      LogUtils.shared.i('Retrieved string: $key = $value');
      return value;
    } catch (e) {
      LogUtils.shared.e('Error retrieving string: $e');
      rethrow;
    }
  }

  // 存储布尔值
  Future<bool> setBool(String key, bool value) async {
    _checkInitialization();
    try {
      final result = await _prefs.setBool(key, value);
      LogUtils.shared.i('Stored bool: $key = $value');
      return result;
    } catch (e) {
      LogUtils.shared.e('Error storing bool: $e');
      rethrow;
    }
  }

  // 获取布尔值
  bool? getBool(String key) {
    _checkInitialization();
    try {
      final value = _prefs.getBool(key);
      LogUtils.shared.i('Retrieved bool: $key = $value');
      return value;
    } catch (e) {
      LogUtils.shared.e('Error retrieving bool: $e');
      rethrow;
    }
  }

  // 存储整数
  Future<bool> setInt(String key, int value) async {
    _checkInitialization();
    try {
      final result = await _prefs.setInt(key, value);
      LogUtils.shared.i('Stored int: $key = $value');
      return result;
    } catch (e) {
      LogUtils.shared.e('Error storing int: $e');
      rethrow;
    }
  }

  // 获取整数
  int? getInt(String key) {
    _checkInitialization();
    try {
      final value = _prefs.getInt(key);
      LogUtils.shared.i('Retrieved int: $key = $value');
      return value;
    } catch (e) {
      LogUtils.shared.e('Error retrieving int: $e');
      rethrow;
    }
  }

  // 删除指定键的数据
  Future<bool> remove(String key) async {
    _checkInitialization();
    try {
      final result = await _prefs.remove(key);
      LogUtils.shared.i('Removed key: $key');
      return result;
    } catch (e) {
      LogUtils.shared.e('Error removing key: $e');
      rethrow;
    }
  }

  // 清除所有数据
  Future<bool> clear() async {
    _checkInitialization();
    try {
      final result = await _prefs.clear();
      LogUtils.shared.i('Cleared all data');
      return result;
    } catch (e) {
      LogUtils.shared.e('Error clearing data: $e');
      rethrow;
    }
  }
}
